package xionglizhen.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import xionglizhen.confilg.XlzInfo;
import xionglizhen.controller.vo.response.FeedBackresponeVo;
import xionglizhen.entity.Illness;
import xionglizhen.entity.Medicine;
import xionglizhen.entity.User;
import xionglizhen.mapper.IllnessMapper;
import xionglizhen.service.IllnessService;
import xionglizhen.service.MedicineService;
import xionglizhen.service.UserService;
import xionglizhen.util.Assert;
import xionglizhen.util.RespResult;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@Controller//该类是控制器

public class IndexController {
    @Autowired
    private XlzInfo xlzInfo;
    @Autowired
    private IllnessService illnessService;
    @Autowired
    private IllnessMapper illnessMapper;
    @Autowired
    private MedicineService medicineService;
    @Autowired
    private UserService userService;
    @RequestMapping("/index")
    //参数非必填，内部实际转为null，所以要求类型是引用类型【自定义数据类型、包装类】

    //如何设置参数的必填【数据合法性】





    public String index(Model model, FeedBackresponeVo feedBackresponeVo){
        System.out.println(feedBackresponeVo);
        System.out.println("表示GET请求/index的URL会到达该方法");
        System.out.println("配置信息："+xlzInfo);
        model.addAttribute("echo",xlzInfo);

        model.addAttribute("username","班级学号姓名");

        FeedBackresponeVo feedBackresponeVo1 =new FeedBackresponeVo();
        feedBackresponeVo1.setName("熊丽珍");
        feedBackresponeVo1.setEmail("23311143@rjgc");
        feedBackresponeVo1.setTitle("留言标题");
        feedBackresponeVo1.setContent("留言详细内容");
        model.addAttribute("feedBackInfo",feedBackresponeVo1);

        HashMap<String,String>hashMap =new HashMap<>();
        hashMap.put("name","熊丽珍");
        hashMap.put("email","23311143@rjgc");
        hashMap.put("title","留言标题");
        hashMap.put("content","留言详细内容");
        model.addAttribute("MapInfo",hashMap);

        return "index";
    }
//退出登录
    @GetMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/index";
    }

    @GetMapping("/profile")
    public String profile(HttpSession session){
        User loginUser= (User) session.getAttribute("loginUser");
        if (Assert.isEmpty(loginUser)){
            return "illness";
        }
        return "profile";

    }



    @RequestMapping("/findIllness")
    public String findIllness101(Model model,String illnessName,@RequestParam(defaultValue = "1")Integer page){
            IPage<Illness> illnessPageDataByIllnessName = illnessService.getIllnessPageDataByIllnessName(illnessName, 10, page);
            model.addAttribute("illness", illnessPageDataByIllnessName.getRecords());
            return "search-illness";
        }

    //药品管理
    @RequestMapping("/all-medical")
    public String allMedical(Model model,String medicineName, @RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer pageSize) {
        List<Medicine> medicinePageDataByIllnessName = medicineService.findMedicineByName(medicineName,page,pageSize);
        model.addAttribute("medicines", medicinePageDataByIllnessName);
        return "all-medical";
    }

    @RequestMapping("/findMedicines")
    public String findMedicines(Model model,String nameValue,@RequestParam(defaultValue = "1") Integer currentPage,@RequestParam(defaultValue = "10") Integer pageSize){
        List<Medicine> medicineList= (List<Medicine>) medicineService.findMedicineByName(nameValue,currentPage,pageSize);
        model.addAttribute("medicineList",medicineList);
        System.out.println("表示GET请求/index的URL会到达");
        return "illness";
    }


    @RequestMapping("/doctor")
    public String doctor(HttpSession session) {
        User loginUser = (User) session.getAttribute("loginUser");
        if (Assert.isEmpty(loginUser)) {
            return "redirect:/index";
        }
        return "doctor";
    }

    @RequestMapping("/feedback.html")
    public String feedback() {
        return "feedback";
    }

    public RespResult reLoginTip() {
        return RespResult.fail("还未登录请登录！");
    }


}

